<?php
include_once "../../../wp-load.php";

function ep_get_postcodes(){
    global $wpdb;
    extract($_GET);
    if(empty($sv)){
        return array();
    }
    return $wpdb->get_results("
        SELECT * FROM postcodes WHERE
        postcode LIKE '%{$sv}%'
        OR sido LIKE '%{$sv}%'
        OR gugun LIKE '%{$sv}%'
        OR dong LIKE '%{$sv}%'
        OR li LIKE '%{$sv}%'
        ORDER BY seq ASC
    ");
}

$postcodes = ep_get_postcodes();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <meta http-equiv="Content-Script-Type" content="text/javascript" />
        <meta http-equiv="Content-Style-Type" content="text/css" />
        <title>search</title>
        <style type="text/css">
            html, body{padding: 0; margin: 0; font-size: 12px; font-family: dotum;}
            body{padding: 5px;}
            form{padding: 5px; text-align: center;}
            a{text-decoration: none; color: #444444;}
            .button, .submit input, .button-secondary {
                background: url("../images/white-grad.png") repeat-x scroll left top #F2F2F2;
                text-shadow: 0 1px 0 #FFFFFF;
            }
            .button, .button-secondary, .submit input, input[type="button"], input[type="submit"] {
                border-color: #BBBBBB;
                color: #464646;
            }
            .submit input, .button, input.button, .button-primary, input.button-primary, .button-secondary, input.button-secondary, .button-highlighted, input.button-highlighted, #postcustomstuff .submit input {
                -moz-box-sizing: content-box;
                border-radius: 11px 11px 11px 11px;
                border-style: solid;
                border-width: 1px;
                cursor: pointer;
                font-size: 11px !important;
                line-height: 13px;
                padding: 3px 8px;
                text-decoration: none;
            }
            #search-postcode-table{width: 100%;}
            #search-postcode-table thead th{padding: 5px; background-color: #EEEEEE;}
            #search-postcode-table tbody td{padding-top: 5px; padding-bottom: 5px; text-align: center;}

            .odd{background-color: #eeeeee;}
        </style>
        <?php
        wp_enqueue_script("jquery");
        print_head_scripts();
        ?>
        <script type="text/javascript">
            //<![CDATA[
            jQuery(document).ready(function($){
                $('a.input-data').click(function(e){
                    var tr = $(this).closest('tr');
                    var code = $('td a.code'  ,tr).text();
                    var address = $('td a.address'  ,tr).text();
                    $("#bd_writer_zip", top.document).val(code);
                    $("#bd_writer_address1", top.document).val(address);
                    self.parent.tb_remove();//thickbox 제거 함수 호출
                });
                //게시판명이 한글일때 닫기버튼 못불러오는거 때문에 이미지 소스 바꿔치기함.
                $("#TB_closeWindowButton img", top.document).attr('src', '/wp-includes/js/thickbox/tb-close.png');
            });
            //]]>
        </script>
    </head>
    <body>
        <form action="<?php echo $_SERVER['REQUEST_URI'] ?>" method="get">
            <input type="search" id="sv" name="sv" value="<?php echo $_GET['sv']; ?>" />
            <input type="submit" value="검색" class="button" />
        </form>
        <table id="search-postcode-table">
            <thead>
                <tr>
                    <th style="width: 60px;">우편번호</th>
                    <th>주소</th>
                </tr>
            </thead>
            <tbody>
                <?php if(empty($postcodes)):?>
                <tr><td colspan="2">검색결과가 없습니다.</td></tr>
                <?php endif; ?>
                <?php 
                    $odd = 0;
                    foreach($postcodes as $p):?>
                <tr <?php echo ($odd % 2 == 1 ? 'class="odd"' : ''); ?>>
                    <td>
                        <a href="#postcode" class="input-data code"><?php echo $p->postcode; ?></a>
                    </td>
                    <td>
                        <a href="#address" class="input-data address"><?php echo $p->sido.' '.$p->gugun.' '.$p->dong.' '.$p->li.' '.$p->bldg.' '.$p->st_bunji; if(!empty($p->st_bunji)){echo '-';} echo $p->ed_bunji; ?></a>
                    </td>
                </tr>
                <?php 
                    $odd++;
                    endforeach;?>
            </tbody>
        </table>
    </body>
</html>
